// pages/personal/appeal.js
const app = getApp()
const pool = new Map()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabs: [{
                label: "待处理",
                status: 1
            },
            {
                label: "已完成",
                status: 0
            },
        ],
		current_status: 1,
		page: 1,
		total: 2,
		loading: false,
		list: [],
    },
    onLoad: function(options) {
        this.loadData()
    },
    tab() {
        if (!pool.has(this.data.current_status)) {
            pool.set(this.data.current_status, {
                page: 1,
                total: 2,
                loading: false,
                list: []
            })
        }
        return pool.get(this.data.current_status)
    },
    clickTab(evt) {
        this.setData({
            current_status: evt.currentTarget.dataset.status
        })
        this.onPullDownRefresh()
    },
    loadData() {
        let tab = this.tab()
        this.setData(tab)
        let {
            orderString = 'createTime.desc', current_status = 1
        } = this.data
        if (tab.list.length >= tab.total || tab.loading) return
        this.setData({
            loading: true
        })
        wx.showLoading({
            title: '加载中...',
        })
		app.http.post('rest/appeal/queryByList.do', {
            data: {
                page: tab.page,
                rows: 10,
                orderString,
				relType:1,
                status:current_status
            }
        }).then(res => {
            tab.page++
            tab.total = res.obj.total
            tab.loading = false
            tab.list.push(...res.obj.rows)
            this.setData(tab)
            wx.hideLoading()
            wx.stopPullDownRefresh()
        }).catch(err => {
            this.setData({
                loading: false
            })
            wx.hideLoading()
        })
    },
    toDetail(evt) {
        let current_status = this.data.current_status
        let id = evt.currentTarget.dataset.id
        let url = ''
        if (current_status == 11) {
            url = '/pages/car-owner/personal/offer-list'
        }
        if (current_status == 12) {
            url = '/pages/car-owner/personal/pay-success'
        }
        if (current_status == 13) {
            url = '/pages/car-owner/personal/order-success'
        }
        wx.navigateTo({
            url: url + '?id=' + id
        })
    },
    onPullDownRefresh: function() {
        const tab = this.tab()
        tab.page = 1
        tab.total = 2
        tab.list = []
        tab.loading = false
        this.loadData()
    },
    onReachBottom: function() {
        this.loadData()
    },
})